package com.chzc.fm.duotin.ui.mine;


import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chzc.fm.duotin.R;
import com.chzc.fm.duotin.ui.other.BaseActivity;
import com.chzc.fm.duotin.ui.other.BaseFragment;


/**
 * 消息中心
 */
public class MsgCenterActivity extends BaseActivity implements View.OnClickListener{

    private int index;
    private int currentIndex;

    private BaseFragment[] fragments;
    private TextView[] textTabs;

    private RelativeLayout rlContainer;
    private TextView textResponse, textCom, textSys;
    @Override
    protected int getLayoutId() {
        return R.layout.activity_msg_center;
    }

    @Override
    protected void initViews() {
        rlContainer = (RelativeLayout) findViewById(R.id.rl_frag_container);
        textResponse = (TextView) findViewById(R.id.tv_msg_response);
        textCom = (TextView) findViewById(R.id.tv_msg_commend);
        textSys = (TextView) findViewById(R.id.tv_msg_sys);
    }

    @Override
    protected void initEvents() {
        textResponse.setOnClickListener(this);
        textCom.setOnClickListener(this);
        textSys.setOnClickListener(this);
    }

    @Override
    protected void init() {
        textResponse.setSelected(true);
        textResponse.setBackgroundResource(R.color.white);

        textTabs = new TextView[]{
                textResponse,
                textCom,
                textSys
        };

        fragments = new BaseFragment[]{
                new MsgFragment(),
                new MsgFragment(),
                new MsgFragment()
        };

        getSupportFragmentManager().beginTransaction()
                .add(R.id.rl_frag_container, fragments[0])
                .add(R.id.rl_frag_container, fragments[1])
                .add(R.id.rl_frag_container, fragments[2])
                .show(fragments[0])
                .hide(fragments[1])
                .hide(fragments[2])
                .commit();
    }



    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_msg_response:
                index = 0;
                onTabIndex(index);
                break;
            case R.id.tv_msg_commend:
                index = 1;
                onTabIndex(index);
                break;
            case R.id.tv_msg_sys:
                index = 2;
                onTabIndex(index);
                break;
        }
    }

    private void onTabIndex(int index) {
        if(currentIndex != index){
            FragmentTransaction trx = getSupportFragmentManager().beginTransaction();

            trx.hide(fragments[currentIndex]);
            trx.show(fragments[index]);
        }
        textTabs[index].setSelected(true);
        textTabs[index].setBackgroundResource(R.color.white);
        textTabs[currentIndex].setSelected(false);
        textTabs[currentIndex].setBackgroundResource(R.color.colorPublic);
        currentIndex = index;
    }
}
